﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]应用程序配置辅助类
*********************************************************************************/
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.Configuration;
using BestEasyCMS.Framework.Extension;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    /// [辅助类]应用程序配置辅助类
    /// </summary>
    public class AppSettingUtil : ConfigurationSection
    {
        /// <summary>
        /// 获取 Web.Config 中的 Setting 集合
        /// </summary>
        public static NameValueCollection AppSettings = WebConfigurationManager.AppSettings;

        /// <summary>
        /// Configuration Object
        /// </summary>
        private static System.Configuration.Configuration _config;


        /// <summary>
        /// 取得字符串类型的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public static string GetString(string key)
        {
            //AppSettings = System.Web.Configuration.WebConfigurationManager.AppSettings;
            //return GetString(key, "");
            return GetCacheValue(key).ToStr();
        }

        /// <summary>
        /// 取得字符串类型的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public static string GetString(string key, string defaultValue)
        {
            return GetCacheValue(key).ToStr().IsEmpty() ? defaultValue : GetCacheValue(key).ToStr();
        }

        /// <summary>
        /// 获得整型类型的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public static int GetInt32(string key)
        {
            return GetInt32(key, 0);
        }

        /// <summary>
        /// 获得整型类型的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public static int GetInt32(string key, int defaultValue)
        {
            //AppSettings = System.Web.Configuration.WebConfigurationManager.AppSettings;//本操作可获取最新的值
            int retValue = defaultValue;

            string value = GetCacheValue(key).ToStr();
            if (!string.IsNullOrEmpty(value))
                int.TryParse(value, out retValue);
            return retValue;
        }

        /// <summary>
        /// 设置配置文件的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetAppSettings(string key, string value)
        {
            _config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

            var appSettings = (AppSettingsSection) _config.GetSection("appSettings");

            if (appSettings.Settings[key] != null)
            {
                appSettings.Settings[key].Value = value;
            }
            else
            {
                appSettings.Settings.Add(key, value);
            }

            _config.Save();
            _config = null;

            AppSettings = WebConfigurationManager.AppSettings;
        }

        /// <summary>
        /// 基于web.config文件的缓存 
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        private static object GetCacheValue(string key)
        {
            key = key.ToLower();
            if (HttpContext.Current.Cache[key] == null)
            {
                var cdy = new CacheDependency(HttpContext.Current.Server.MapPath("~/web.config"));
                if (WebConfigurationManager.AppSettings[key] != null)
                {
                    HttpContext.Current.Cache.Insert(key, WebConfigurationManager.AppSettings[key], cdy);
                }
                else
                {
                    throw new Exception("在web.config的appSettings里找不到{0}键名！".FormatWith(key));
                }
            }

            return HttpContext.Current.Cache[key];
        }
    }
}